package com.learn.algorithm.unionfind;

import java.io.*;

// https://www.nowcoder.com/practice/e7ed657974934a30b2010046536a5372
public class UnionFind {

    public static int MAX = 1000001;
    public static int[] father = new int[MAX];
    public static int[] size = new int[MAX];
    public static int[] stack = new int[MAX];

    public static int n;

    public static void build() {
        for (int i = 0; i <= n; i++) {
            father[i] = i;
            size[i] = 1;
        }
    }

    public static int find(int i) {
        int size = 0;
        while (i != father[i]) {
            // 记录沿途进过的节点
            stack[size++] = i;
            i = father[i];
        }
        // 扁平化沿途进过的节点, 使沿途经过的节点的father都指向i
        while (size > 0) {
            father[stack[--size]] = i;
        }
        return i;
    }

    public static boolean isSameSet(int a, int b) {
        return find(a) == find(b);
    }

    public static void union(int a, int b) {
        // 找到a/b的代表元素
        int fa = find(a);
        int fb = find(b);
        if (fa == fb) { // 代表元素相同 不必合并
            return;
        }
        // 找到元素数少的集合 将其挂到元素数多的集合上 能够降低高度 加速查找
        if (size[fa] >= size[fb]) {
            father[fb] = fa;
            size[fa] += size[fb];
        } else {
            father[fa] = fb;
            size[fb] += size[fa];
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        while (in.nextToken() != StreamTokenizer.TT_EOF) {
            n = (int) in.nval;
            build();
            in.nextToken();
            int m = (int) in.nval;
            for (int i = 0; i < m; i++) {
                in.nextToken();
                int op = (int) in.nval;
                in.nextToken();
                int x = (int) in.nval;
                in.nextToken();
                int y = (int) in.nval;
                if (op == 1) {
                    out.println(isSameSet(x, y) ? "Yes" : "No");
                } else {
                    union(x, y);
                }
            }
        }
        out.flush();
        out.close();
        br.close();
    }
}
